﻿using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models;
using RndMaterialDisposalService.Repository;
using RndMaterialDisposalService.Services.Interface;
using System.Linq;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace RndMaterialDisposalService.Services
{
    /// <summary>
    /// 
    /// </summary>
    [NAutowired(Lifecycle.Scoped)]
    public class UserInfoService : IUserInfoService
    {
        private EDPMiddleContext dbContext;
        private UserBaseInforDataContext dbUserBaseContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_dbContext"></param>
        public UserInfoService(EDPMiddleContext _dbContext, UserBaseInforDataContext _dbUserBaseContext)
        {
            dbContext = _dbContext;
            dbUserBaseContext = _dbUserBaseContext;
        }

        /// <summary>
        /// 获取员工详细信息
        /// </summary>
        /// <param employeeNumber="员工号"></param>
        /// <returns></returns>
        public async Task<K2CloudGetEmployeeInfoModel> GetEmployeeInfo(string employeeNumber)
        {
            return await dbContext.K2CloudGetEmployeeInfo.FirstOrDefaultAsync(p=>p.Emp_Num.Equals(employeeNumber));
        }
        /// <summary>
        /// 获取员工基本信息
        /// </summary>
        /// <param employeeNumber="员工号"></param>
        /// <returns></returns>
        public async Task<(List<UserBaseInforModel>,int)> GetUserInfoList(string? keyWord, int pageIndex = 1, int pageSize = 10)
        {
            var query = dbUserBaseContext.UserBaseInfor.Where(x => x.Status == 1);
            if (!string.IsNullOrEmpty(keyWord))
            {
                query = query.Where(x => ((!string.IsNullOrEmpty(x.EmployeeNumber) && x.EmployeeNumber.ToUpper().Contains(keyWord.ToUpper())))
                || (!string.IsNullOrEmpty(x.UserName.ToUpper()) && x.UserName.Contains(keyWord.ToUpper()))
                || (!string.IsNullOrEmpty(x.Email) && x.Email.ToUpper().Contains(keyWord.ToUpper()))
                || (!string.IsNullOrEmpty(x.PinyinName) && x.PinyinName.ToUpper().Contains(keyWord.ToUpper())));
            }
            var totalCount = query.Count();
            var data = await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
            return (data,totalCount);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<UserBaseInforModel> GetUserInfo(string userId)
        {
            return await dbUserBaseContext.UserBaseInfor.FirstOrDefaultAsync(x => x.UserId == userId);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public async Task<UserBaseInforModel> GetUserInfoByAccount(string userAccount)
        {
            return await dbUserBaseContext.UserBaseInfor.FirstOrDefaultAsync(x => x.UserAccount.ToLower() == userAccount.ToLower());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employeeNumber"></param>
        /// <returns></returns>
        public async Task<UserBaseInforModel> GetUserDetail(string employeeNumber)
        {
            return await dbUserBaseContext.UserBaseInfor.FirstOrDefaultAsync(x => x.EmployeeNumber == employeeNumber);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employeeNumber">员工ID</param>
        /// <returns>员工部门领导信息</returns>
        public async Task<UserBaseInforModel> GetUserDeptLeader(string employeeNumber)
        {
            var empDetail = await dbContext.K2CloudGetEmployeeInfo.FirstOrDefaultAsync(p => p.Emp_Num!.Equals(employeeNumber));
            var deptLeaderNum = empDetail?.Supervisor_EmployeeNumber ?? "";
            var deptLeaderEmail = empDetail?.Supervisor_Email ?? "";
            return await dbUserBaseContext.UserBaseInfor.FirstOrDefaultAsync(x => (x.EmployeeNumber == deptLeaderNum || x.Email == deptLeaderEmail));
        }
    }
}
